﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;
using Telerik.Web.UI;
using System.Collections.Generic;
using EHTWebsiteProject.Code.Entity;

namespace EHTWebsiteProject.Administrator
{
    public partial class ListProduct : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private Int32 danhmucID = -1;
        private String languageCode = "vi-VN";
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));
        private String Search = "";
        private String tenDanhMucVN = "";
        private String tenDanhMucOther = "";
        private DataTable dtProductCategorys = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserName"] == null)
                    Response.Redirect("../Account/EHTLogin.aspx");
                else
                {
                    dtProductCategorys = DanhMucSanPhamProvider.GetDanhMucSanPhams(-1, "", "").Tables[0];

                    try
                    {
                        languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                        if (languageCode == "en-us")
                        {
                            IsVN = false;
                        }

                    }
                    catch (Exception ex)
                    {
                    }
                    try
                    {
                        danhmucID = Int32.Parse(Request.QueryString["DanhMucID"]);
                    }
                    catch
                    {

                    }

                    try
                    {
                        Search = Request.QueryString["txtSearch"].ToString();
                    }
                    catch
                    {

                    }
                    Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                    
                    //hiện thị cha và con
                    RadTreeList1.ExpandToLevel(1);
                    RadTreeList1.ExpandToLevel(2);

                    RadTreeList2.ExpandToLevel(1);
                    RadTreeList2.ExpandToLevel(2);

                    CreateDDLCategory(0, 0);
                    if (IsVN == true)
                        this.ddlDanhmucSanPham.Items.Insert(0, new ListItem(HangSo.DanhMuc, "0"));
                    else
                        this.ddlDanhmucSanPham.Items.Insert(0, new ListItem(HangSo.DanhMucOther, "0"));
                    ddlDanhmucSanPham.SelectedValue = danhmucID.ToString();

                    // hiện thị theo ngôn ngữ
                    if (IsVN == true)
                        pnNhomOther.Visible = false;
                    else
                        pnNhom.Visible = false;

                }
            }
        }

        /// <summary>
        /// Ham load danh muc san pham
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CreateDDLCategory(Int32 ChuyenMucChaID, Int32 level)
        {
            String preString = "";

            for (Int32 i = 0; i < level; ++i)
            {
                preString += "--";
            }

            DataRow[] drCategorysSub = dtProductCategorys.Select("ChuyenMucChaID = " + ChuyenMucChaID.ToString());
            Int32 amountOfCategorySub = drCategorysSub.Count();

            //Điểm dừng đệ quy
            if (amountOfCategorySub == 0)
                return;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                for (Int32 i = 0; i < amountOfCategorySub; ++i)
                {
                    Int32 subCategoryID = Int32.Parse(drCategorysSub[i]["DanhMucID"].ToString());
                    ListItem li = new ListItem(preString + drCategorysSub[i]["TenDanhMucVN"].ToString(), drCategorysSub[i]["DanhMucID"].ToString());
                    this.ddlDanhmucSanPham.Items.Add(li);
                    CreateDDLCategory(subCategoryID, level + 1);
                }
            }
            else
            {
                for (Int32 i = 0; i < amountOfCategorySub; ++i)
                {
                    Int32 subCategoryID = Int32.Parse(drCategorysSub[i]["DanhMucID"].ToString());
                    ListItem li = new ListItem(preString + drCategorysSub[i]["TenDanhMucOther"].ToString(), drCategorysSub[i]["DanhMucID"].ToString());
                    this.ddlDanhmucSanPham.Items.Add(li);
                    CreateDDLCategory(subCategoryID, level + 1);
                }
            }
        }

        /// <summary>
        /// Ngon ngu tieng viet
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        protected void RadTreeList1_PageSizeChanged(object sender, Telerik.Web.UI.TreeListPageSizeChangedEventArgs e)
        {
            RadTreeList1.DataSource = DanhMucSanPhamProvider.GetDanhMucSanPhams(danhmucID, tenDanhMucVN, tenDanhMucOther);
            RadTreeList1.DataBind();
        }

        protected void RadTreeList1_PageIndexChanged(object sender, Telerik.Web.UI.TreeListPageChangedEventArgs e)
        {
            RadTreeList1.CurrentPageIndex = e.NewPageIndex;
            RadTreeList1.DataSource = DanhMucSanPhamProvider.GetDanhMucSanPhams(danhmucID, tenDanhMucVN, tenDanhMucOther);
            RadTreeList1.DataBind();
        }

        protected void RadTreeList1_NeedDataSource(object sender, Telerik.Web.UI.TreeListNeedDataSourceEventArgs e)
        {
            if (IsVN == true)
                tenDanhMucVN = Search;
            else
                tenDanhMucOther = Search;
            RadTreeList1.DataSource = DanhMucSanPhamProvider.GetDanhMucSanPhams(danhmucID, tenDanhMucVN, tenDanhMucOther);
        }

        protected void RadTreeList1_ItemCommand(object sender, Telerik.Web.UI.TreeListCommandEventArgs e)
        {
            if (e.CommandName == RadTreeList.ExpandCollapseCommandName)
            {
                RadTreeList1.DataSource = DanhMucSanPhamProvider.GetDanhMucSanPhams(danhmucID, tenDanhMucVN, tenDanhMucOther);
                RadTreeList1.DataBind();
            }
        }

        /// <summary>
        /// Ngon ngu tieng anh
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RadTreeList2_ItemCommand(object sender, TreeListCommandEventArgs e)
        {
            if (e.CommandName == RadTreeList.ExpandCollapseCommandName)
            {
                RadTreeList2.DataSource = DanhMucSanPhamProvider.GetDanhMucSanPhams(danhmucID, tenDanhMucVN, tenDanhMucOther);
                RadTreeList2.DataBind();
            }
        }

        protected void RadTreeList2_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            if (IsVN == true)
                tenDanhMucVN = Search;
            else
                tenDanhMucOther = Search;
            RadTreeList2.DataSource = DanhMucSanPhamProvider.GetDanhMucSanPhams(danhmucID, tenDanhMucVN, tenDanhMucOther);
        }

        protected void RadTreeList2_PageIndexChanged(object sender, TreeListPageChangedEventArgs e)
        {
            RadTreeList2.CurrentPageIndex = e.NewPageIndex;
            RadTreeList2.DataSource = DanhMucSanPhamProvider.GetDanhMucSanPhams(danhmucID, tenDanhMucVN, tenDanhMucOther);
            RadTreeList2.DataBind();
        }

        protected void RadTreeList2_PageSizeChanged(object sender, TreeListPageSizeChangedEventArgs e)
        {
            RadTreeList2.DataSource = DanhMucSanPhamProvider.GetDanhMucSanPhams(danhmucID, tenDanhMucVN, tenDanhMucOther);
            RadTreeList2.DataBind();
        }


        protected void btnselect_Click(object sender, EventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (TreeListDataItem item in RadTreeList1.SelectedItems)
                {
                    String str = item["DanhMucID"].Text;
                    DanhMucSanPham theDanhMucSp = DanhMucSanPhamProvider.GetDanhMucSanPhamByDanhMucID(Int32.Parse(str));
                    arr.Add(str);                   
                }

            }
            else
            {
                foreach (TreeListDataItem item in RadTreeList2.SelectedItems)
                {
                    String str = item["DanhMucID"].Text;
                    DanhMucSanPham theDanhMucSp = DanhMucSanPhamProvider.GetDanhMucSanPhamByDanhMucID(Int32.Parse(str));
                     arr.Add(str); 
                }
            }
            if (arr.Count > 1 || arr.Count == 0)
            {
                if (IsVN == true)
                    ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Edit_VN);
                else
                    ltrErorr.Text = HelpMethod.ErorrLogin(HangSo.Eorr_Add);
                return;
            }
            else
            {
                Session["value"] = Int32.Parse(arr[0].ToString());
                Page.ClientScript.RegisterStartupScript(this.GetType(), "myCloseScript", "GetRadWindow().close()", true);                                
            }
        }

        protected void ImagSearch_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("ListProduct.aspx?DanhMucID=" + ddlDanhmucSanPham.SelectedValue);
        }

        protected void ddlDanhmucSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("ListProduct.aspx?DanhMucID=" + ddlDanhmucSanPham.SelectedValue);
        }

    }
}